package cn.onein.edu.manage.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.onein.edu.manage.domain.ContentTags;
import cn.onein.edu.manage.domain.LearnCategory;
import cn.onein.edu.manage.domain.LearnContent;
import cn.onein.edu.manage.domain.LearnSection;
import cn.onein.edu.manage.service.LearnContentService;

public class LearnContentServiceImpl extends HibernateDaoSupport implements
		LearnContentService {

	@SuppressWarnings("unchecked")
	public List<LearnSection> findSection() {
		List<LearnSection> list = this.getHibernateTemplate().find(
				"From LearnSection");

		return list;
	}

	@SuppressWarnings("unchecked")
	public List<LearnCategory> findCategorysBySection(LearnSection section) {
		List<LearnCategory> categorys = this.getHibernateTemplate().find(
				"From LearnCategory where section = ?", section);

		return categorys;
	}

	public Boolean save(final LearnContent content) {
		return getHibernateTemplate().execute(new HibernateCallback<Boolean>() {
			@Override
			public Boolean doInHibernate(Session session)
					throws HibernateException, SQLException {
				Transaction tx = null;
				try {
					tx = session.beginTransaction();
					
					if(content.getTags() != null){
						content.setTags(content.getTags().trim());
						content.setTags(content.getTags().replaceAll("  "," "));
						content.setTags(content.getTags().replaceAll("  "," "));
						content.setTags(content.getTags().replaceAll("  "," "));
						content.setTags(content.getTags().replaceAll("关键词",""));
						content.setTags(content.getTags().replaceAll(":",""));
						content.setTags(content.getTags().replaceAll("：",""));
						content.setTags(content.getTags().replaceAll("，",","));
						content.setTags(content.getTags().replaceAll(" ",","));						
					} 
					else{
						content.setTags("");
					}
					
					content.setHits(0);
					content.setFavorites(0);
					content.setEnable(true);
					content.setFullText(content.getFullText().replaceAll("　", ""));
					content.setTitle(content.getTitle().trim());
					
					if(content.getAuthor() != null) content.setAuthor(content.getAuthor().trim());
					if(content.getRefUrl() != null) content.setRefUrl(content.getRefUrl().trim());
					
					content.setFullText(content.getFullText().replaceAll("&nbsp;", ""));
					
					session.save(content);
					
					String tags = content.getTags();
					String[] tag = tags.split(",");
					for (String o : tag) {
						if (!"".equals(o.trim())) {
							String hql = "From ContentTags where tagName = ?";
							Query query = session.createQuery(hql);
							query.setParameter(0, o.trim());
							if (query.list().isEmpty()) {
								ContentTags contentTag = new ContentTags();
								contentTag.setTagName(o.trim());
								contentTag.setUsedNum(1);
								contentTag.setContentIds(content.getId()
										.toString());
								session.save(contentTag);
							} else {
								ContentTags contentTag = (ContentTags) query
										.list().get(0);
								contentTag
										.setUsedNum(contentTag.getUsedNum() + 1);
								contentTag.setContentIds(contentTag
										.getContentIds() +  "," 
										+ content.getId().toString());
								session.update(contentTag);
							}
						}
					}
					tx.commit();
				} catch (Exception ex) {
					tx.rollback();
					ex.printStackTrace();
				}

				return true;
			}
		});

	}
}
